<%@page import="org.w3c.dom.NodeList"%>
<%@page import="org.w3c.dom.Node"%>
<%@page import="org.w3c.dom.Element"%>
<%@page import="org.w3c.dom.Document"%>
<%@page import="javax.xml.parsers.DocumentBuilder"%>
<%@page import="javax.xml.parsers.DocumentBuilderFactory"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.net.HttpURLConnection"%>
<%@page import="java.net.URL"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%!//类型分类
	public String getURLDate(String strUrl) {
		String rtn = "";
		try {
			URL url = new URL(strUrl);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setRequestProperty("User-agent", "Mozilla/4.0");
			connection.connect();
			InputStream is = connection.getInputStream();
			BufferedReader l_reader = new BufferedReader(new InputStreamReader(
					is, "UTF-8"));
			String sCurrentLine = "";
			while ((sCurrentLine = l_reader.readLine()) != null) {
				rtn += sCurrentLine;
			}
			return rtn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtn;
	}

public String getXML(String url){
	try {
	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	DocumentBuilder db = dbf.newDocumentBuilder();
	Document doc = db.parse(url);
	return doc.toString();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}

	//读取XML
	public Map<String,Object> readXml(String url) {
		Map<String,Object> rtnMap = new HashMap<String,Object>();
		int total = 0;
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(url);
			Element root = doc.getDocumentElement();
			//获取总数量
			NodeList summaryList = root.getElementsByTagName("summary");
			if(summaryList.getLength()>0){
				String totalNum = summaryList.item(0).getTextContent().trim();
				total = Integer.parseInt(totalNum);
			}
			List<Map<String,String>> list = new ArrayList<Map<String,String>>();
			if(total>0){
				rtnMap.put("total",total);
				NodeList listNode = root.getElementsByTagName("list");
				Element infoEl = (Element) listNode.item(0);
				NodeList infoList = infoEl.getElementsByTagName("info");
				
				for (int i = 0; i < infoList.getLength(); i++) {
					Map<String,String> m = new HashMap<String,String>();
					Element info = (Element) infoList.item(i);//遍历集合获取每个学生元素
	
					String infoid = info.getElementsByTagName("infoid").item(0).getTextContent();//获取infoid
					m.put("infoid",infoid);
					String title = info.getElementsByTagName("title").item(0).getTextContent();//获取title
					m.put("title",title);
					
					String url2 = info.getElementsByTagName("url").item(0).getTextContent();//获取title
					m.put("url",url2);
					
					NodeList parasList = info.getElementsByTagName("paras");
					Element paras = (Element)parasList.item(0);
					String goblianxiren = paras.getElementsByTagName("goblianxiren").item(0).getTextContent();
					m.put("goblianxiren",goblianxiren);
					
					NodeList imagesList = info.getElementsByTagName("images");
					if(imagesList.getLength()>0){
	// 					String image = imagesList.item(0).getFirstChild().getTextContent();
						Element images = (Element)imagesList.item(0);
						String image = images.getElementsByTagName("image").item(0).getTextContent();
						m.put("image",image);
					}else{
						m.put("image","http://foshan.smartcity.com.cn/gate/images/fzimages/index/58service_default.jpg");
					}
					
					
					String postdate = info.getElementsByTagName("postdate").item(0).getTextContent();//获取infoid
					m.put("postdate",postdate);
					String content = info.getElementsByTagName("content").item(0).getTextContent();//获取infoid
					m.put("content",content);
					list.add(m);
				}
			}
			rtnMap.put("list",list);
			return rtnMap; 
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}%>
